package com.dayuanit.dy15.spider.task;

import com.dayuanit.dy15.spider.dto.ThirdInfoDTO;
import com.dayuanit.dy15.spider.entity.Navigation;
import com.dayuanit.dy15.spider.mapper.NavigationMapper;
import com.dayuanit.dy15.spider.util.ApiConnector;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 处理导航栏的任务
 */
@Component
public class NavTask extends BaseTask {

    @Autowired
    private NavigationMapper navigationMapper;

    @Override
    public void doTask() {
        //抓取的起始地址 相当于是种子地址
        String html = ApiConnector.get("http://e.dangdang.com/list-XS2-dd_sale-0-1.html");

        Document document = Jsoup.parse(html);

        //获取导航栏的div
        Elements dlElements = document.select("div.first_level.publication.publisher");
        dlElements.forEach(element -> {
            String text = element.select("a").first().text();
            System.out.println(">" + text);

            //保存第一级的导航栏
            Navigation firstNav = save(0, text, 1, 0L);
            Elements lis = element.select("ul.second_level").select("li");
            lis.forEach(e2 -> {
                if (e2.select("a").first() != null) {
                    String title = e2.select("a").first().text();
                    String url = e2.select("a").first().attr("href");
                    System.out.println(">>>" + title + " " + url);

                    //保存二级导航栏
                    Navigation secondNav = save(0, title, 2, firstNav.getId());

                    Elements el = e2.select("ul.third_level").select("a");
                    el.forEach(e3 -> {
                        String text3 = e3.text();
                        String url3 = e3.attr("href");

                        //保存第三级导航栏地址
                        thirdUrls.add(new ThirdInfoDTO(url3, text3));

                        //保存第三级目录   一袋佳人
                        save(1, text3, 3, secondNav.getId());
                        System.out.println(">>>>>>" + text3 + " " + url3);
                    });
                }
            });
        });
    }


    private Navigation save(int leaf, String navName, int leave, Long parentId) {
        Navigation navigation = new Navigation();
        navigation.setLeaf(leaf);
        navigation.setNavName(navName);
        navigation.setLevel(leave);
        navigation.setParentNavId(parentId);
//        navigationMapper.insert(navigation);
        return navigation;
    }
}
